package com.example.ordersys.controller;

import com.example.ordersys.config.AppFinal;
import com.example.ordersys.mapper.OrderDetailMapper;
import com.example.ordersys.mapper.OrderInfoMapper;
import com.example.ordersys.model.OrderInfo;
import com.example.ordersys.model.UserInfo;
import com.example.ordersys.tools.ResponseBody;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderInfoController {

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @RequestMapping("/add")
    @Transactional
    public ResponseBody<Integer> add(String dids, HttpServletRequest request) {
        int data = -1;
        int status=-1;
        String message="下单失败";
        //检查登录
        if (AppFinal.getUserBySession(request)==null){
            message="请先登录";
            return new ResponseBody<>(status, message, data);
        }
        // 1.添加订单信息，返回一个订单 ID
            OrderInfo  orderInfo = new OrderInfo();
            orderInfo.setUid(AppFinal.getUserBySession(request).getId());
            // 订单添加方法
            int result = orderInfoMapper.add(orderInfo);
            if (result > 0) {
                // 2.添加订单详情(需要订单ID)
                data = orderDetailMapper.add(orderInfo.getId(),dids.substring(0,dids.length()-1).split(","));
                status=0;
                message="";

            }else{
                throw new RuntimeException(message);
            }

        return new ResponseBody<>(status, message, data);
    }

    @RequestMapping("/list")
    public ResponseBody<List<OrderInfo>> getList(HttpServletRequest request){
        int status=-1;
        String message="获取订单列表失败";
        List<OrderInfo> data = null;
        if (AppFinal.getUserBySession(request)==null){
            message="请先登录";
            return new ResponseBody<>(status,message,data);
        }
          if((data=orderInfoMapper.getList(AppFinal.getUserBySession(request).getId()))!=null){
              status=0;
              message="";
          };
        return new ResponseBody<>(status,message,data);
    }
//
    @RequestMapping("/alllist")
    public ResponseBody<List<OrderInfo>> getAllList(HttpServletRequest request){
        int status=-1;
        String message="获取订单列表失败";

        List<OrderInfo> data =null;
        if ((data=orderInfoMapper.getAllList())!=null){
            status=0;
            message="";
        }
        return new ResponseBody<>(status,message,data);
    }
    //修改订单状态
    @RequestMapping("/up")
    public ResponseBody<Integer> up(int oid,int status1){
        int data = 0;
        int status=-1;
        String message="订单操作失败";
        if ((data = orderInfoMapper.up(oid,status1))>0){
            status=0;
            message="订单操作成功";
        }

        return new ResponseBody<>(status,message,data);
    }

}